package controllers;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import models.Adpo;
import models.Book;
import models.BookType;
import models.Manager;
import models.Member;
import models.LeaMes;
import models.News;
import play.mvc.Controller;
import service.AdpoService;
import service.BookService;
import service.LeaMesService;
import service.NewsService;
public class IndexAction extends Controller{
	
	private static enum ORDER {
		desc,asc
	};
	
	public static void index(){
		List<BookType> bookTypes = null;/*一级分类--关联下级*/
		Member user = null;
		List<Book> 	hootPin = null/*热评读书*/,
					newBook = null/*新书上架*/,
					enshtBook = null/*收藏榜*/,
					brooBook = null/*借阅榜*/ ,
					gytszg = null  /*扎文学*/ ,
					gysejd = null ,
					gytses = null ;
					
		
		Adpo ads	= null,/*首页下面的图片广告*/
			 hd		= null;/*首页幻灯*/
		
		List<Member> axb = null;
		try{
			bookTypes = BookType.find("fid is null").fetch();
			//给当前分类加载子集分类
			List<BookType> newTypes = new ArrayList<BookType>();
			for(int i = 0 ; i < bookTypes.size() ;i ++){
				BookType b = bookTypes.get(i);
				b = BookType.setSun(b);
				newTypes.add(b);
			}
			bookTypes = newTypes;
			hootPin = BookService.getHootPin(6);
			newBook = BookService.getNewBook(10);
			enshtBook = BookService.getEnshBook(10);
			brooBook = BookService.getBorrBook(11);
			gytszg = BookService.allBook("1");
			gysejd = BookService.allBook("1");
			gytses = BookService.allBook("1");
			String userid = session.get("userid");
			try{
				user = Member.findById(userid);
			}catch(Exception e){}
			//广告加载--begin
			hd = AdpoService.findByCode("syhd", 7);
			ads = AdpoService.findByCode("sytp0001", 1);
			//广告加载--end
			
			//爱心榜
			axb = Member.find("order by booknum desc").fetch(10);
			
			
		}catch(Exception e){e.printStackTrace();}
		render(bookTypes,hootPin,newBook,enshtBook,brooBook,user,hd,ads,axb,gytszg,gysejd,gytses);
	}
	
	/**
	 * 前台会员/游客留言
	 * @param lemDet  留言内容
	 */
	public static void createLeas(String lemDet){
		String memId = session.get("userid"); //拿到会员的id
		if(lemDet == null || "".equals(lemDet.trim())){
			flash.success("留言添加失败!");
		}else{
			LeaMes lea = new LeaMes();
			if( session.get("userid") == null){
				lea.member = null;
			}
			lea.member = Member.findById(Long.parseLong(session.get("userid"))); 
			lea.lemDet = lemDet.trim();
			lea.lemTime= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(
						 new Date(System.currentTimeMillis())).toString();
			LeaMesService.create(lea);
			flash.success("留言添加成功！");
		}
		index();
	}
	//根据新闻栏目code读取相应的新闻类容
	public static void news(String code){
		News newses = null;
		try{
			newses = NewsService.findByCode(code,1).get(0);
			System.out.println(newses+"----------------------------------");
		}catch(Exception e){e.printStackTrace();}
		render("/IndexAction/lhfc.html",code,newses);
	}
	//企业管--begin
	public static void ent_index(){
		render("/Enterprise/index.html");
	}
	
	public static void enterprise_show(String name){
		System.err.println(name);
		render("/Enterprise/show.html");
	}
	
	public static void sqCompany(){
		
	}
	//企业管--end
	
	//shop--beg
	public static void books_list(String id,Integer cp,String[] param,String xl,String df,String pl,String jg,String sjs){
		
		Integer rp = 10;
		Integer count = 0;
		cp = cp == null ? 1 : cp;
		
		
		Integer pc = (count+rp-1)/rp;
		List<Book> books = null;
		List<BookType> bookTypes = null;/*一级分类--关联下级*/
		List<Book> 	brooBook = null;/*借阅榜*/
		try{
			if(id == null)
				bookTypes = BookType.find("fid is null").fetch();
			else
				bookTypes = BookType.find("id = ?",Long.parseLong(id)).fetch();
			//给当前分类加载子集分类
			List<BookType> newTypes = new ArrayList<BookType>();
			for(int i = 0 ; i < bookTypes.size() ;i ++){
				BookType b = bookTypes.get(i);
				b = BookType.setSun(b);
				newTypes.add(b);
			}
			bookTypes = newTypes;
			
			brooBook = BookService.getBorrBook(11);
			if(id == null)
				books =  Book.find("").fetch(cp, 10);
			else{
				String ids = BookService.getSunType(Long.parseLong(id));
				if(ids != null && ids.length() > 0){
					ids = ids.substring(1)+","+id;
				}else{
					ids=id;
				}
				//图书加载区域------begin
				String query = "";
				System.err.println(ids+"%%%%%%%%%%%%%%%%%%%%%%%");
				books =  Book.find("bookTypeIds in ("+ids+")").fetch(cp, 10);
			}
			//图书------------end
			count = books.size();
		}catch(Exception e){
			e.printStackTrace();
		}
		render(bookTypes,books);
	}
	//shop--end
	
	//帮助中心
	
	public static void help(String code){
		System.out.println(code+"--------------------------------");
		News newses = null;
		try{
			newses = NewsService.findByCode(code,1).get(0);
			System.out.println(newses+"----------------------------------");
		}catch(Exception e){}
		render("/IndexAction/help.html",code,newses);
	}
	
	//帮助中心 end
	
	//搜索
	public static void search(){
		render();
	}
	//搜索 end
	
	public static void donatebook(){
		 String userid = session.get("userid");
		 if(userid == "" || userid == null){
			 render("/proscenium/memlog.html");
		 }
		render("/IndexAction/donatebook.html");
	}
	
	
	
}
